package org.jeecg.modules.friends;

/**
 * @author cold
 * @data 2024/6/23 00:38
 * @version 1.0
 */
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.apache.commons.collections4.CollectionUtils;
import org.jeecg.modules.friends.auth.service.IUserAuthenticationService;
import org.jeecg.modules.friends.dao.InvitedUserDAO;
import org.jeecg.modules.friends.dao.ReadyToDeleteDAO;
import org.jeecg.modules.friends.dao.SystemConfigDAO;
import org.jeecg.modules.friends.dao.UserPicsDAO;
import org.jeecg.modules.friends.friendsinfo.service.IFriendsInfoService;
import org.jeecg.modules.friends.model.ReadyToDeleteDO;
import org.jeecg.modules.friends.moments.mapper.MomentsMapper;
import org.jeecg.modules.friends.moments.service.IMomentsService;
import org.jeecg.modules.friends.momentspics.service.IMomentsPicsService;
import org.jeecg.modules.friends.service.ReadyToDeleteService;
import org.jeecg.modules.friends.service.UserReportService;
import org.jeecg.modules.friends.service.VipUserService;
import org.jeecg.modules.friends.user.entity.User;
import org.jeecg.modules.friends.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Maps;

import lombok.extern.slf4j.Slf4j;

@EnableScheduling
@Component
@Slf4j
public class LogoffScheduleTask {

    @Autowired
    private ReadyToDeleteService readyToDeleteService;
    @Autowired
    private IUserService userService;

    @Autowired
    private UserPicsDAO userPicsDAO;
    @Autowired
    private IUserAuthenticationService userAuthenticationService;

    @Autowired
    private IFriendsInfoService friendsInfoService;

    @Autowired
    private VipUserService vipUserService;

    @Autowired
    private InvitedUserDAO invitedUserDAO;

    @Autowired
    private UserReportService userReportService;
    @Autowired
    private IMomentsService momentsService;

    @Scheduled(fixedRate = 5000) // 每隔5秒执行一次
    public void deleteCurrentTime() {
        log.info("开始删除用户....");
        List<ReadyToDeleteDO> deleteDOList = readyToDeleteService.queryPastTimeRecords();
        if (CollectionUtils.isNotEmpty(deleteDOList)) {
            log.info("删除数量:{}", deleteDOList.size());
            Set<Long> userIdSet = deleteDOList.stream().map(ReadyToDeleteDO::getUserId)
                    .collect(Collectors.toSet());
            LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
            userLambdaQueryWrapper.in(User::getId, userIdSet);
            List<User> userList = userService.list(userLambdaQueryWrapper);
            Map<Long, User> userMap = Maps.newHashMap();
            if (CollectionUtils.isNotEmpty(userList)) {
                userMap = userList.stream().collect(Collectors.toMap(User::getId, Function.identity()));
            }
            for (ReadyToDeleteDO readyToDeleteDO : deleteDOList) {
                User user = userMap.get(readyToDeleteDO.getUserId());
                if (user == null) {
                    continue;
                }
                friendsInfoService.deleteByUserId(user.getId());
                momentsService.deleteByUserId(user.getId());
                userPicsDAO.deleteByUserId(user.getId());
                invitedUserDAO.deleteByUserId(user.getId());
                vipUserService.deleteByUserId(user.getId());
                userReportService.deleteByUserId(user.getId());
                userAuthenticationService.deleteByUserId(user.getId());
                userService.removeById(user);
            }
            readyToDeleteService.updateFinishedBatch(deleteDOList);
            log.info("删除完成");
        }
    }
}
